<?php
namespace app\mobile\controller;

use app\BaseController;

use app\common\AES;
use app\model\Order as SelfModel;
use app\model\Member;
use app\model\DistributionType;
use app\model\GoodsPackage;
use app\model\Goods;
use app\model\Order2;
use app\model\OrderGoods;
use app\model\GoodsPackageType;
use app\model\OrderGoods2;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Cookie;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;
class Order extends Base
{


    public function index($list_rows = 6)
    {
        $param = Request::param();
        return view('', ['data' => [], 'list_rows' => $list_rows]);
    }

    public function view($id)
    {
        $data = SelfModel::find($id);
        return view('', ['info' => $data]);
    }

    public function ajax($list_rows=2)
    {
        $param = Request::param();
        $data = SelfModel::withSearch(array_keys($param), $param)
            ->where('member_id',$this->member_id)
            ->order(['sort' => 'asc', 'id' => 'desc'])
            ->group('order_number')
            ->with('OrderGoods')
            ->paginate($list_rows)->append(['status_text']);
        return view('',['data'=>$data,'l'=>$param['status']]);
        //return json(['code' => 200, 'message' => '最新数据', 'data' => $data]);
    }

    /**
     * 创建订单
     */
    public function create()
    {
        $dat = $this->param['data'];
        $data = (new AES())->decrypt($dat);

        foreach ($data['data'] as $v) {

            $order_data = [
                'type_id' => 2,
                'member_id' => $this->member_id,
                'shop_id' => $v['shop_id'],
                'order_number' => order_number(),
                'prov' => $data['address']['prov'],
                'city' => $data['address']['city'],
                'area' => $data['address']['area'],
                'name' => $data['address']['name'],
                'mobile' => $data['address']['mobile'],
                'address' => $data['address']['address'],
                'order_price' => $data['total_price'],
                'goods_count' => $data['total_num'],
                'status' => 1
            ];

            $order = \app\model\Order::create($order_data);
            foreach ($v['list'] as $v2) {
                $goods_data = [
                    'shop_id' => $v2['shop_id'],
                    'order_id' => $order['id'],
                    'order_number' => $order['order_number'],
                    'goods_id' => $v2['goods_id'],
                    'title' => $v2['title'],
                    'price' => $v2['price'],
                    'attr_values' => $v2['attr_values'],
                    'line_price' => $v2['line_price'],
                    'single_pic' => $v2['single_pic'],
                    'number_price' => $v2['number_price'],
                    'number' => $v2['number']
                ];
                $order_goods = OrderGoods::create($goods_data);
            }

        }

        return json(['code'=>200,'msg'=>'下单成功','data'=>$order]);

    }

    public function tui()
    {
        $param = Request::param();
        $data = SelfModel::withSearch(array_keys($param), $param)
            ->where('member_id', $this->member_id)
            ->where('id', $param['order_id'])
            ->with('OrderGoods')
            ->find();

        return view('',['data'=>$data]);
    }

    public function tui_price($tui_price = 0, $order_id = 0)
    {
        $status = SelfModel::where('id', $order_id)->value('status');
        $order_price = SelfModel::where('id', $order_id)->value('order_price');

        if($tui_price <= 0){
            return json(['code' => 400, 'message' => '请填写退款金额']);
        }
        if($status != 2){
            return json(['code' => 400, 'message' => '当前不是付款状态']);
        }
        if($tui_price > $order_price){
            return json(['code' => 400, 'message' => '退款金额大于订单金额']);
        }
        $ret = SelfModel::where('id', $order_id)->update(['status' => 8]);
        if ($ret) {
            return json(['code' => 200, 'message' => '退款申请中']);
        }
        return json(['code' => 400, 'message' => '失败']);
    }


}
